package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.CnocOsJobBeanConfig;
import com.soft.idc.common.util.JQPage;

public class ComponentManagerController extends Controller {
	public void index() {
		this.render("/perf_kpi_manager/component.html");
	}
	
	public void queryComponent() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		StringBuffer sb = new StringBuffer();
		String bean_class = this.getPara("bean_class");
		sb.append("select c.id, c.bean_class, c.bean_class_path, c.state, ");
		sb.append("case(state) when '10A' then '在用' when '10P' then '停用' end as state_name, ");
		sb.append("c.create_date, c.comment, c.url_path ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append("from cnoc_os_job_bean_config c where 1=1 ");
		
		if(bean_class !=null ){
			sqlFrom.append(" and c.bean_class like '%" + bean_class + "%' ");
		}
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		System.out.println(JsonKit.toJson(jPage));
		this.renderJson(jPage);
	}
	
	public void add() {
		String bean_class = this.getPara("bean_class");
		String bean_class_path = this.getPara("bean_class_path");
		String state = this.getPara("state");
		String comment = this.getPara("comment");
		String url_path = this.getPara("url_path");
		Date now = new Date();
		CnocOsJobBeanConfig component = new CnocOsJobBeanConfig();
		component.setBeanClass(bean_class);
		component.setBeanClassPath(bean_class_path);
		component.setSTATE(state);
		component.setCOMMENT(comment);
		component.setUrlPath(url_path);
		component.setCreateDate(now);
		boolean result = component.save();
		this.renderJson(result);
	}
	
	public void delete() {
		final String ids = this.getPara("ids");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] idArr = ids.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("delete from cnoc_os_job_bean_config where id in(");
				for(int i=0; i<idArr.length; i++){
					if(i==idArr.length-1){
						sb.append(idArr[i] + ")");
					}else{
						sb.append(idArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==idArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}
	
	public void edit() {
		String id = this.getPara("id");
		String bean_class = this.getPara("bean_class");
		String bean_class_path = this.getPara("bean_class_path");
		String state = this.getPara("state");
		String comment = this.getPara("comment");
		String url_path = this.getPara("url_path");
		CnocOsJobBeanConfig component = new CnocOsJobBeanConfig();
		component = component.findById(Integer.parseInt(id));
		component.setBeanClass(bean_class);
		component.setBeanClassPath(bean_class_path);
		component.setSTATE(state);
		component.setCOMMENT(comment);
		component.setUrlPath(url_path);
		boolean result = component.update();
		this.renderJson(result);
	}
}
